// 导入用户集合
const { Article } = require('../../../model/article');
const { Comment } = require('../../../model/comment');

module.exports = async (req, res) => {
    const { id } = req.params
  const article = await Article.findOne({_id: id}).populate('category author')
  const comments = await Comment.find({article: id}).populate('user').sort({_id: -1})
  // 统计浏览量
  Article.updateOne({_id: id}, {
    views: article.views + 1
  }, (err, row) => {
    if(!err) {
      res.json({
          data: {
			  article,
			  comments
		  },
      })
    } else {
      res.status(403)
      res.json({
        log: err,
        message: '统计浏览量失败！'
      })
    }
  })
}